import { mapGetters, mapActions } from "vuex";
import Popup from "vux/src/components/popup/index.vue";

import _ from "lodash";
import mipai from "./miPai/miPai";
import { decrypt, encryption } from "@/assets/js/http";

import template from "./roomTitleTemplate";
const { render, staticRenderFns } = template;

export default {
  render,
  staticRenderFns,
  props: ["roomName1", "timeMessage", "showLotList", "newLotResult"],
  data() {
    return {
      getIssue: false,
      popupNavShow: false,
      lottery_type: this.$route.query.lottery_type,
      roomTitles: {
        1: "幸运28",
        2: "北京PK10",
        3: "加拿大28",
        4: "幸运飞艇",
        5: "重庆时时彩",
        6: "三分彩",
        7: "香港六合彩",
        8: "急速六合彩",
        9: "急速赛车",
        10: "百人牛牛",
        11: "分分彩",
        13: "骰宝",
        14: "分分PK10",
      },
      lotteryList: [], //开奖结果
      beginSlide: true, //开奖结果开始动
      ranDomNum: [],
      tsFlag: false, //是否停售
      tsDlg: false,
      stopMsg: "", //停售信息
      lottRstHtml: "",
      slideTimer: {},
      timers: {
        one: 0,
        two: 0,
        three: 0,
        four: 0,
        five: 0,
        six: 0,
        one1: 0,
        two1: 0,
        three1: 0,
        four1: 0,
        five1: 0,
        six1: 0,
      },
      timer: null,
      timer1: null,
      timersShow: 0,

      lotteryTypeFun: false,
      lotteryData: [],
      roomInfo: {},
      loadingMoney: false,
      isLhc: false,
      typeArr: [],
      activeTypeIndex: 0,
      activeTypeItem: "",
      lhcListTl: {},
      showHeroList: false,
    };
  },
  created() {},
  mounted() {
    this.$http
      .post(this.urlRequest + "?m=win&c=lobby&a=getRoomBaseConfig", {
        token: localStorage.getItem("token"),
      })
      .then((result) => {
        if (result.status == 0) {
          this.showHeroList = result.data.bet_rank_sw ? true : false;
        }
      });
    var roomTop = document.getElementById("roomTop");
    roomTop.ontouchmove = function (e) {
      e.preventDefault();
      e.stopPropagation();
    };
    if (
      this.lottery_type == 1 ||
      this.lottery_type == 3 ||
      this.lottery_type == 17 ||
      this.lottery_type == 18 ||
      this.lottery_type == 20 ||
      this.lottery_type == 22
    ) {
      this.typeArr = ["总和", "号码"];
    } else if (
      this.lottery_type == 2 ||
      this.lottery_type == 4 ||
      this.lottery_type == 9 ||
      this.lottery_type == 14
    ) {
      this.typeArr = ["号码", "大小", "单双", "冠亚龙虎"];
    } else if (
      this.lottery_type == 5 ||
      this.lottery_type == 6 ||
      this.lottery_type == 11 ||
      this.lottery_type == 21 ||
      this.lottery_type == 23
    ) {
      this.typeArr = ["号码", "大小", "单双", "总和"];
    } else if (this.lottery_type == 7 || this.lottery_type == 8 || this.lottery_type == 24) {
      this.typeArr = ["号码", "总和", "特码"];
      this.isLhc = true;
    } else if (this.lottery_type == 10) {
      this.typeArr = ["号码", "大小", "单双", "总和"];
    } else if (this.lottery_type == 13 || this.lottery_type == 19) {
      this.typeArr = ["号码", "大小", "单双", "总和"];
    }
    this.getLottery(this.typeArr[0], 0);
    this.getLotteryType();
  },
  computed: {
    ...mapGetters(["roomData", "accountData", "service"]),
  },
  methods: {
    ...mapActions(["SET_ACCOUNT_DATA"]),
    refreshMoney: function () {
      this.loadingMoney = true;
      this.SET_ACCOUNT_DATA({
        Obj: {},
      });
      setTimeout(() => {
        this.loadingMoney = false;
      }, 800);
    },
    lotteryTypeToggle: function () {
      //彩种列表开关
      if (!this.lotteryType) {
        this.getLotteryType();
      }
      if (this.lotteryTypeFun) {
        this.lotteryTypeFun = false;
      } else {
        this.lotteryTypeFun = true;
      }
    },
    getLotteryType: function () {
      //获取彩种房间
      this.$http
        .post(this.urlRequest + "?m=api&c=lobby&a=lotteryType")
        .then((result) => {
          if (_.isEmpty(result)) {
            return;
          }
          this.lotteryData = result.data;
        });
    },
    goLotteryRoom(id) {
      //去彩种房间
      if (!localStorage.getItem("token")) {
        var that = this;
        this.$vux.alert.show({
          content: "您还未登录，请先登录",
          onHide() {
            that.$router.push({
              path: "/login",
            });
          },
        });
        return;
      }
      var params = {
        token: localStorage.getItem("token"),
        lottery_type: id,
      };
      this.lotteryTypeFun = false;
      this.$vux.loading.show();
      this.$http
        .post(this.urlRequest + "?m=api&c=openAward&a=instantlyBetting", params)
        .then((res) => {
          this.$vux.loading.hide();
          if (res.status == 0) {
            this.roomInfo = res.data;
            console.log(this.roomInfo);

            this.$router.replace({
              path: "/room/" + this.roomInfo.room_id,
              query: {
                lottery_type: this.roomInfo.lottery_type,
                id: this.roomInfo.room_id,
              },
            });

            location.reload();
          } else {
            if (res.ret_msg && res.ret_msg != "") {
              this.$vux.toast.show({
                text: res.ret_msg,
              });
            }
          }
        })
        .catch((error) => {
          this.$vux.loading.hide();
          this.$vux.toast.show({
            text: "数据请求超时",
          });
        });
    },
    getLottery: function (item, index) {
      this.activeTypeItem = item;
      var obj = {
        号码: 1,
        大小: 2,
        单双: 3,
        总和: 4,
        特码: 5,
        冠亚龙虎: 6,
      };
      this.$http
        .post(this.urlRequest + "?m=api&c=openAward&a=roomDataList", {
          lottery_type: this.lottery_type,
        })
        .then((result) => {
          if (_.isEmpty(result)) {
            return false;
          }
          this.lhcListTl = result.list[0];
          if (this.lottery_type == 7 || this.lottery_type == 8 || this.lottery_type == 24) {
            this.lhcListTl.lastNum = result.list[0].open_result.split(",")[
              result.list[0].open_result.split(",").length - 1
            ];
            this.lhcListTl.open_result = result.list[0].open_result.slice(
              0,
              result.list[0].open_result.length - 1
            );
            this.lhcListTl.lastStr =
              result.list[0].spare_2[result.list[0].spare_2.length - 1];
            this.lhcListTl.spare_2.pop();
          }
        });
      // 问题2：开奖信息没有fixed
      this.$http
        .post(this.urlRequest + "?m=api&c=openAward&a=roomOpenAwardList", {
          lottery_type: this.lottery_type,
          type: obj[item],
        })
        .then((result) => {
          if (_.isEmpty(result)) {
            return false;
          }
          this.lotteryList = result.list;

          this.activeTypeIndex = index;
        });
    },
    setLotteryList: function () {
      if (this.showLotList) {
        this.$emit("setLotList", false);
      } else {
        this.$emit("setLotList", true);
      }
    },
    slideNum: function () {
      setTimeout(() => {
        this.beginSlide = false;
      }, 3000);
    },
    showLotteryText: function (num) {
      let dxString = [];
      if (num <= 13) {
        dxString.push("小");
      } else {
        dxString.push("大");
      }
      if (num % 2 == 0) {
        dxString.push("双");
      } else {
        dxString.push("单");
      }
      return dxString.join(",");
    },
    goHome: function () {
      this.$router.push({
        path: "/home",
      });
    },
    headBack: function () {
      if (this.roomData.roomListLen == 1) {
        this.$router.push({
          path: "/home",
        });
      } else {
        this.$router.push({
          path: "/roomList",
          query: { lottery_type: this.$route.query.lottery_type },
        });
      }
    },
    showNumImg(num) {
      var colorArr = [
        "pk10NumBox1",
        "pk10NumBox2",
        "pk10NumBox3",
        "pk10NumBox4",
        "pk10NumBox5",
        "pk10NumBox6",
        "pk10NumBox7",
        "pk10NumBox8",
        "pk10NumBox9",
        "pk10NumBox10",
      ];
      var sleNum = parseInt(num) - 1;
      var result = colorArr[sleNum];
      return result;
    },
    showSlideNum(index) {
      if (this.lottery_type == 7 || this.lottery_type == 8 || this.lottery_type == 24) {
        if (this.beginSlide) {
          return "slideNum1";
        } else {
          return "lhcSlideEnd" + (index + 1);
        }
      } else {
        if (this.beginSlide) {
          return "slideNum";
        } else {
          return "slideEnd" + (index + 1);
        }
      }
    },
    specificPoke(name) {
      var s;
      var nameArr = name.replace(/(\d+|[ajqk])/i, "-$1").split("-");
      if (nameArr[0] == "黑桃") {
        s = 1;
      }
      if (nameArr[0] == "红心") {
        s = 2;
      }
      if (nameArr[0] == "梅花") {
        s = 3;
      }
      if (nameArr[0] == "方块") {
        s = 4;
      }
      return ["poker" + nameArr[1] + "_" + s];
    },
    setRandomNum: function () {
      var that = this;
      this.slideTimer = setInterval(function () {
        if (
          that.lottery_type == 1 ||
          that.lottery_type == 3 ||
          that.lottery_type == 17 ||
          that.lottery_type == 18 ||
          that.lottery_type == 20 ||
          that.lottery_type == 22
        ) {
          let newArr = [];
          for (var i = 0; i < 4; i++) {
            newArr.push(parseInt(27 * Math.random()));
          }
          that.ranDomNum = newArr;
        } else if (
          that.lottery_type == 2 ||
          that.lottery_type == 4 ||
          that.lottery_type == 9 ||
          that.lottery_type == 14
        ) {
          let newArr = [];
          for (var i = 0; i < 10; i++) {
            newArr.push(Math.floor(10 * Math.random() + 1));
          }
          that.ranDomNum = newArr;
        } else if (
          that.lottery_type == 5 ||
          that.lottery_type == 6 ||
          that.lottery_type == 11 ||
          that.lottery_type == 21 ||
          that.lottery_type == 23
        ) {
          let newArr = [];
          for (var i = 0; i < 5; i++) {
            newArr.push(parseInt(9 * Math.random()));
          }
          that.ranDomNum = newArr;
        } else if (that.lottery_type == 7 || that.lottery_type == 8 || this.lottery_type == 24) {
          let newArr = [];
          for (var i = 0; i < 7; i++) {
            newArr.push(Math.floor(49 * Math.random() + 1));
          }
          that.ranDomNum = newArr;
        } else if (that.lottery_type == 13 || this.lottery_type == 19) {
          let newArr = [];
          for (var i = 0; i < 3; i++) {
            newArr.push(Math.floor(5 * Math.random() + 1));
          }
          that.ranDomNum = newArr;
        }
      }, 100);
    },
    closeBet: function () {
      var obj = this.roomData;
      obj.showPmDlg = false;
      obj.notZhuiHao = false;
      obj.showAskRoad = false;
      this.$store.commit("setRoomData", obj);
    },
    addBetTimer(data) {
      var tzTime = parseInt(data.time) - parseInt(data.sealingTim);
      var fpTime = parseInt(data.sealingTim);
      if (parseInt(data.time) < parseInt(data.sealingTim)) {
        fpTime = parseInt(data.time);
      }
      var that = this;
      clearInterval(that.timer);
      this.timer = null;
      this.timer = setInterval(() => {
        if (tzTime > 0) {
          //投注中
          this.timersShow = 0;
          var obj = this.roomData;
          obj.stopBet = false;
          this.$store.commit("setRoomData", obj);
          if (tzTime > 0) {
            tzTime--;
            this.setTimer(tzTime);
          }
        } else if (fpTime > 0 && tzTime <= 1) {
          this.timers.one = 0;
          this.timers.two = 0;
          this.timers.three = 0;
          this.timers.four = 0;
          this.timers.five = 0;
          this.timers.six = 0;
          this.timersShow = 1;
          var obj = this.roomData;
          obj.stopBet = true;
          this.$store.commit("setRoomData", obj);
          fpTime--;
          this.setTimer(fpTime, true);
        }
      }, 1000);
    },
    setTimer: function (time, flag) {
      let hourTime = 0;
      let minTime = 0;
      let senTime = 0;
      if (time < 1 && flag) {
        this.timersShow = 2;
        var obj = this.roomData;
        obj.stopBet = true;
        this.$store.commit("setRoomData", obj);
      }
      if (time <= 60) {
        this.timers.one = parseInt(time % 10);
        this.timers.two = parseInt(time / 10);
        this.timers.three = 0;
        this.timers.four = 0;
        this.timers.five = 0;
        this.timers.six = 0;
      } else if (time > 60 && time < 60 * 60) {
        minTime = parseInt(time / 60.0);
        senTime = parseInt(
          (parseFloat(time / 60.0) - parseInt(time / 60.0)) * 60
        );
        this.timers.two = parseInt(senTime / 10);
        this.timers.one = parseInt(senTime % 10);
        this.timers.four = parseInt(minTime / 10);
        this.timers.three = parseInt(minTime % 10);
        this.timers.five = 0;
        this.timers.six = 0;
      } else if (time >= 60 * 60) {
        hourTime = parseInt(time / 3600.0);
        minTime = parseInt(
          (parseFloat(time / 3600.0) - parseInt(time / 3600.0)) * 60
        );
        senTime = parseInt(
          (parseFloat(
            (parseFloat(time / 3600.0) - parseInt(time / 3600.0)) * 60
          ) -
            parseInt(
              (parseFloat(time / 3600.0) - parseInt(time / 3600.0)) * 60
            )) *
            60
        );
        this.timers.two = parseInt(senTime / 10);
        this.timers.one = parseInt(senTime % 10);
        this.timers.four = parseInt(minTime / 10);
        this.timers.three = parseInt(minTime % 10);
        this.timers.six = parseInt(hourTime / 10);
        this.timers.five = parseInt(hourTime % 10);
      }
    },
  },
  components: {
    mipai,
    Popup,
  },
  watch: {
    newLotResult: function (newVal, oldVal) {
      if (!_.isEmpty(newVal)) {
        this.getLottery(this.activeTypeItem, this.activeTypeIndex);
        return false;
        if (
          this.lottery_type == 3 ||
          this.lottery_type == 1 ||
          this.lottery_type == 17 ||
          this.lottery_type == 18 ||
          this.lottery_type == 20 ||
          this.lottery_type == 22
        ) {
          let data = {
            issue: newVal.issue,
            open_result: newVal.result.slice(9, 11),
            spare_1: newVal.result.slice(0, 8),
          };

          this.lotteryList.unshift(data);
        } else if (
          this.lottery_type == 2 ||
          this.lottery_type == 4 ||
          this.lottery_type == 9 ||
          this.lottery_type == 14
        ) {
          let data = {
            issue: newVal.issue,
            open_result: newVal.result,
            open_time: newVal.open_time,
            spare_2: "",
          };
          console.log(data)
          console.log('----')
          this.lotteryList.unshift(data);
        } else if (
          this.lottery_type == 5 ||
          this.lottery_type == 6 ||
          this.lottery_type == 11 ||
          this.lottery_type == 21 ||
          this.lottery_type == 23
        ) {
          let data = {
            issue: newVal.issue,
            open_result: newVal.result,
            open_time: newVal.open_time,
            spare_2: newVal.sum_result_str.split(","),
          };
          this.lotteryList.unshift(data);
        } else if (this.lottery_type == 10) {
          let data = {
            issue: newVal.issue,
            open_result_another: newVal.sum_result_str,
            who_win: newVal.sum_result_str.substr(0, 3) == "红方胜" ? 1 : 0,
            win_pai:
              newVal.sum_result_str.substr(0, 3) == "红方胜"
                ? newVal.result.split("|")[1].slice(3).split(",")
                : newVal.result.split("|")[0].slice(3).split(","),
            vs: {
              blue: {
                niu: newVal.niu.blue_niu,
                pai: newVal.result.split("|")[0].slice(3).split(","),
              },
              red: {
                niu: newVal.niu.red_niu,
                pai: newVal.result.split("|")[1].slice(3).split(","),
              },
            },
          };
          this.lotteryList.unshift(data);
        } else if (this.lottery_type == 13 || this.lottery_type == 19) {
          let data = {
            issue: newVal.issue,
            open_result: newVal.result,
            open_time: newVal.open_time,
            spare_2: newVal.sum_result_str.split(","),
          };
          this.lotteryList.unshift(data);
        }
        if (this.roomData.showMiPai) {
          var that = this;
          setTimeout(function () {
            if (document.getElementById("lottRst")) {
              var rstHtml = document.getElementById("lottRst").innerHTML;
            }
            that.lottRstHtml = rstHtml;
          }, 1000);
        } else {
          this.slideNum();
          this.beginSlide = true;
        }
        this.lotteryList.pop();
      }
    },
    timeMessage: function (newVal, oldVal) {
      if (newVal.stopOrSell == 2) {
        this.tsFlag = true;
        this.tsDlg = true;
        this.stopMsg = newVal.stopMsg;
      }
      var time = parseInt(newVal.time);

      this.addBetTimer(newVal);
      this.getIssue = true;
    },
    roomData: {
      handler(newVal, oldVal) {
        if (_.isEmpty(this.lotteryList[0])) {
          return;
        }
        if (newVal.showMiPai) {
          this.beginSlide = false;
          var that = this;
          setTimeout(function () {
            if (document.getElementById("lottRst")) {
              var rstHtml = document.getElementById("lottRst").innerHTML;
            }
            that.lottRstHtml = rstHtml;
          }, 100);
        }
      },
      deep: true,
    },
    lotteryList: function (newVal, oldVal) {
      if (!_.isEmpty(newVal)) {
        this.slideNum();
        this.setRandomNum();
        if (
          (this.lottery_type == 7 || this.lottery_type == 8 || this.lottery_type == 24) &&
          this.activeTypeIndex == 0
        ) {
          for (var i = 0; i < newVal.length; i++) {
            newVal[i].lastNum = newVal[i].open_result.split(",")[
              newVal[i].open_result.split(",").length - 1
            ];
            newVal[i].open_result = newVal[i].open_result.slice(
              0,
              newVal[i].open_result.length - 1
            );
            newVal[i].lastStr = newVal[i].spare_2[newVal[i].spare_2.length - 1];
            newVal[i].spare_2.pop();
          }
        }
      }
    },
    beginSlide: {
      handler(newVal, oldVal) {
        if (newVal == false) {
          clearInterval(this.slideTimer);
        }
      },
      deep: true,
    },
  },
  filters: {
    issueFilter(input) {
      if (input && input.length > 10) {
        return input.slice(4, input.length);
      } else {
        return input;
      }
    },
  },
  deactivated() {},
  beforeDestroy() {
    var obj = this.roomData;
    obj.showMiPai = false;
    obj.stopBet = false;
    clearInterval(this.timer);
    this.$store.commit("setRoomData", obj);
  },
};

// WEBPACK FOOTER //
// ./src/components/room/roomTitle/roomTitle.vue
